Skip to main content

Java

These rule examples will help your developers to write better Java code. Use them as inspiration to create your own guideline repository.

## Code
- Handle exceptions appropriately. Use checked exceptions for recoverable conditions and unchecked exceptions for programming errors. Avoid catching generic exceptions like Exception or Throwable.
- Follow Java naming conventions, such as CamelCase for classes and methods and lowerCamelCase for variables. Consistency improves readability.
- Use logging libraries (e.g., SLF4J, Log4j, java.util.logging) for logging errors and important events. Use structured logging to improve searchability.
- Leverage Java annotations (e.g., @RolesAllowed, @PreAuthorize) for declarative security checks when using frameworks like Spring Security.

## Documentation & testing
- Organize tests into distinct categories, ensuring unit tests are isolated and integration tests validate the interactions between components. Use annotations to clearly differentiate between unit tests (@Test) and integration tests (consider using @SpringBootTest for Spring applications).
- Use mocking frameworks (e.g., Mockito) to simulate dependencies in unit tests, ensuring tests are isolated and repeatable.
- Utilize Javadoc comments to document public classes, methods, and fields. Include descriptions of parameters, return types, and exceptions.